﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.LIB;

namespace YJKFamilyLibManager.FamilyLibModule
{
	public partial class DlgSelectCategory : System.Windows.Forms.Form
	{
		public TreeNode CurTreeNode
		{
			get
			{
				return this.mCurTreeNode;
			}
			set
			{
				this.mCurTreeNode = value;
			}
		}

		public DlgSelectCategory(TreeNode trRoot)
		{
			this.mTreeRoot = trRoot;
			this.mCurTreeNode = null;
			this.InitializeComponent();
		}

		private void GetRealTreeNode()
		{
			TreeNode[] array = this.mTreeRoot.Nodes.Find(this.mCurTreeNode.Text, true);
			for (int i = 0; i < array.Length; i++)
			{
				TreeNode treeNode = array[i];
				string text = treeNode.Text;
				while (treeNode.Parent != this.mTreeRoot)
				{
					treeNode = treeNode.Parent;
					text = treeNode.Text + "\\" + text;
				}
				if (text == this.mCurTreeNode.FullPath)
				{
					this.mCurTreeNode = array[i];
					return;
				}
			}
		}

		private void trCategory_DoubleClick(object sender, EventArgs e)
		{
			if (this.mCurTreeNode != null)
			{
				this.GetRealTreeNode();
				base.DialogResult = DialogResult.OK;
			}
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			if (this.mCurTreeNode == null)
			{
				MessageBox.Show("还没有选择分类，请先选择分类。");
				return;
			}
			this.GetRealTreeNode();
		}

		private void DlgSelectCategory_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			foreach (object obj in this.mTreeRoot.Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				TreeNode treeNode2 = new TreeNode(treeNode.Name);
				this.SetChildNode(treeNode, treeNode2);
				this.trCategory.Nodes.Add(treeNode2);
			}
			this.trCategory.ExpandAll();
		}

		private void SetChildNode(TreeNode trFrom, TreeNode trTo)
		{
			foreach (object obj in trFrom.Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				TreeNode treeNode2 = new TreeNode(treeNode.Name);
				this.SetChildNode(treeNode, treeNode2);
				trTo.Nodes.Add(treeNode2);
			}
		}

		private void trCategory_AfterSelect(object sender, TreeViewEventArgs e)
		{
			this.mCurTreeNode = e.Node;
		}

		private TreeNode mTreeRoot;

		private TreeNode mCurTreeNode;
	}
}
